package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MachineAbnormalConstant;
import com.tbit.main.constant.MachineStateConstant;
import com.tbit.main.constant.OrderCumConstant;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;

/**
 * 运营分析 单个区域详细
 *
 * @author LMY
 * @create 2021-01-13 10:41
 */
@RestController
@RequestMapping("/operationReport")
public class OperationReportController {
    @Autowired
    private NewUserDayStaService newUserDayStaService;
    @Autowired
    private RideOrderDayStaService rideOrderDayStaService;
    @Autowired
    private DepositOrderDayStaService depositOrderDayStaService;
    @Autowired
    private VipCardOrderDayStaService vipCardOrderDayStaService;
    @Autowired
    private OperationHourStaService operationHourStaService;
    @Autowired
    private UserService userService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private MemberLogService memberLogService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MachineAbnormalService machineAbnormalService;
    @Autowired
    private AccountConfigService accountConfigService;
    @Autowired
    private MachineStatusService machineStatusService;
    @Autowired
    private OrderCumStaService orderCumStaService;
    @Autowired
    private VipLogService vipLogService;
    @Autowired
    private MachineFaultService machineFaultService;
    @Autowired
    private OperationBriefingService operationBriefingService;

    /**
     * 运营区域总信息
     */
    @RequestMapping("/userMachineCount")
    public JsonResult userMachineCount(String token, Integer accountId) {
        //用户总数
        Integer userCount = userService.getUserCount(accountId);
        Map<String, Object> map = new HashMap<>();
        map.put("userCount", userCount);

        //免押会员总数
        Integer memberUserCount = userService.getDepositUserCount(accountId, DateTimeUtil.getNowTimeYMD());
        map.put("memberUserCount", memberUserCount);

        //投放车辆总数
        Integer machineCount = machineService.getCountByAccountId(accountId);
        if (machineCount == null) {
            map.put("machineCount", 0);
        } else {
            map.put("machineCount", machineCount);
        }
        map.put("machineUserCount", operationBriefingService.selectOperationalMachineCountByAccountIdOrRegionId(accountId, null));
        return JsonResult.succ(map);
    }

    /**
     * 骑行里程统计总数
     */
    @RequestMapping("/mileageCount")
    public JsonResult mileageCount(String token, Integer accountId,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime
    ) {

        Integer mileageCount = newUserDayStaService.getAccountId(accountId, startTime, endTime);

        if (endTime == null || endTime.substring(0, 10).equals(DateTimeUtil.getNowTimeYMD())) {
            Integer mileageNew = rideLogService.newMile(accountId, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", DateTimeUtil.getNowTime());
            mileageCount = mileageCount + mileageNew;
        }
        return JsonResult.succ(mileageCount);
    }

    /**
     * 骑行订单统计总数
     */
    @RequestMapping("/rideOrderCount")
    public JsonResult rideOrderCount(String token, Integer accountId,
                                     @RequestParam(value = "startTime", required = false) String startTime,
                                     @RequestParam(value = "endTime", required = false) String endTime) {
        List<Integer> accountIds = new LinkedList<>();
        accountIds.add(accountId);

        OrderCumSta orderCumSta = new OrderCumSta();
        if (startTime == null) {
            orderCumSta = orderCumStaService.getByAccountIdDateDay(accountIds, OrderCumConstant.QX_ORDER);
            if (orderCumSta.getOrderCount() == null) {
                orderCumSta.setOrderCount(0);
            }
            if (orderCumSta.getOrderMoney() == null) {
                orderCumSta.setOrderMoney((float) 0.00);
            } else {
                orderCumSta.setOrderMoney((float) (orderCumSta.getOrderMoney() / 100));
            }
        } else {
            Map<String, Object> orderCumStaMap = rideOrderDayStaService.getByAccountIds(accountIds, startTime, endTime);
            if (orderCumStaMap != null) {
                orderCumSta.setOrderCount(Integer.parseInt(orderCumStaMap.get("orderCount").toString()));
                orderCumSta.setOrderMoney((float) Float.parseFloat(orderCumStaMap.get("orderMoney").toString()) / 100);
            } else {
                orderCumSta.setOrderCount(0);
                orderCumSta.setOrderMoney((float) 0.00);
            }
        }

        RideSta rideStaOrderCumSta = new RideSta();
        if (endTime == null || endTime.substring(0, 10).equals(DateTimeUtil.getNowTimeYMD())) {
            rideStaOrderCumSta = rideLogService.staByTimeByAccountIds(accountIds, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", DateTimeUtil.getNowTime());

            if (rideStaOrderCumSta.getOrderCount() == null) {
                rideStaOrderCumSta.setOrderCount(0);
            }
            if (rideStaOrderCumSta.getOrderMoney() == null) {
                rideStaOrderCumSta.setOrderMoney(0.00);
            } else {
                rideStaOrderCumSta.setOrderMoney((double) rideStaOrderCumSta.getOrderMoney() / 100);
            }

            orderCumSta.setOrderCount(orderCumSta.getOrderCount() + rideStaOrderCumSta.getOrderCount());
            orderCumSta.setOrderMoney((new BigDecimal(String.valueOf(orderCumSta.getOrderMoney())).add(new BigDecimal(String.valueOf(rideStaOrderCumSta.getOrderMoney())))).floatValue());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("orderCount", orderCumSta.getOrderCount());
        map.put("orderMoney", orderCumSta.getOrderMoney());
        return JsonResult.succ(map);
    }

    /**
     * 免押订单统计总数
     */
    @RequestMapping("/depositOrderCount")
    public JsonResult depositOrderCount(String token, Integer accountId,
                                        @RequestParam(value = "startTime", required = false) String startTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        List<Integer> accountIds = new LinkedList<>();
        accountIds.add(accountId);

        OrderCumSta orderCumStaMemberSta = new OrderCumSta();
        if (startTime == null) {
            orderCumStaMemberSta = orderCumStaService.getByAccountIdDateDay(accountIds, OrderCumConstant.MY_ORDER);
            if (orderCumStaMemberSta.getOrderCount() == null) {
                orderCumStaMemberSta.setOrderCount(0);
            }
            if (orderCumStaMemberSta.getOrderMoney() == null) {
                orderCumStaMemberSta.setOrderMoney((float) 0.00);
            } else {
                orderCumStaMemberSta.setOrderMoney((float) orderCumStaMemberSta.getOrderMoney() / 100);
            }
        } else {
            Map<String, Object> orderCumStaMemberStaMap = depositOrderDayStaService.getByAccountIds(accountIds, startTime, endTime);
            if (orderCumStaMemberStaMap != null) {
                orderCumStaMemberSta.setOrderCount(Integer.parseInt(orderCumStaMemberStaMap.get("orderCount").toString()));
                orderCumStaMemberSta.setOrderMoney((float) Float.parseFloat(orderCumStaMemberStaMap.get("orderMoney").toString()) / 100);
            }
        }

        MemberSta OrderCumSta = new MemberSta();
        if (endTime == null || endTime.substring(0, 10).equals(DateTimeUtil.getNowTimeYMD())) {
            OrderCumSta = memberLogService.staByTimeByAccountIds(accountIds, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", DateTimeUtil.getNowTime());

            if (OrderCumSta.getMemberOrderCount() == null) {
                OrderCumSta.setMemberOrderCount(0);
            }
            if (OrderCumSta.getMemberOrderMoney() == null) {
                OrderCumSta.setMemberOrderMoney(0.00);
            } else {
                OrderCumSta.setMemberOrderMoney((double) OrderCumSta.getMemberOrderMoney() / 100);
            }

            orderCumStaMemberSta.setOrderCount(orderCumStaMemberSta.getOrderCount() + OrderCumSta.getMemberOrderCount());
            orderCumStaMemberSta.setOrderMoney((new BigDecimal(String.valueOf(orderCumStaMemberSta.getOrderMoney())).add(new BigDecimal(String.valueOf(OrderCumSta.getMemberOrderMoney())))).floatValue());

        }


        Map<String, Object> map = new HashMap<>();
        map.put("orderCount", orderCumStaMemberSta.getOrderCount());
        map.put("orderMoney", orderCumStaMemberSta.getOrderMoney());
        return JsonResult.succ(map);
    }

    /**
     * 会员卡订单统计总数
     */
    @RequestMapping("/vipOrderCount")
    public JsonResult vipOrderCount(String token, Integer accountId,
                                    @RequestParam(value = "startTime", required = false) String startTime,
                                    @RequestParam(value = "endTime", required = false) String endTime) {
        List<Integer> accountIds = new LinkedList<>();
        accountIds.add(accountId);

        OrderCumSta orderCumStaMemberSta = new OrderCumSta();
        if (startTime == null) {
            orderCumStaMemberSta = orderCumStaService.getByAccountIdDateDay(accountIds, OrderCumConstant.VIP_ORDER);
            if (orderCumStaMemberSta.getOrderCount() == null) {
                orderCumStaMemberSta.setOrderCount(0);
            }
            if (orderCumStaMemberSta.getOrderMoney() == null) {
                orderCumStaMemberSta.setOrderMoney((float) 0.00);
            } else {
                orderCumStaMemberSta.setOrderMoney((float) orderCumStaMemberSta.getOrderMoney() / 100);
            }
        } else {
            Map<String, Object> orderCumStaVipStaMap = vipCardOrderDayStaService.getByAccountIds(accountIds, startTime, endTime);
            if (orderCumStaVipStaMap != null) {
                orderCumStaMemberSta.setOrderCount(Integer.parseInt(orderCumStaVipStaMap.get("orderCount").toString()));
                orderCumStaMemberSta.setOrderMoney((float) Float.parseFloat(orderCumStaVipStaMap.get("orderMoney").toString()) / 100);
            }
        }

        VipSta orderVipSta = new VipSta();
        if (endTime == null || endTime.substring(0, 10).equals(DateTimeUtil.getNowTimeYMD())) {
            orderVipSta = vipLogService.getCountAccountIds(accountIds, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", DateTimeUtil.getNowTime());

            if (orderVipSta.getVipOrderCount() == null) {
                orderVipSta.setVipOrderCount(0);
            }
            if (orderVipSta.getVipOrderMoney() == null) {
                orderVipSta.setVipOrderMoney(0.00);
            } else {
                orderVipSta.setVipOrderMoney((double) orderVipSta.getVipOrderMoney() / 100);
            }

            orderCumStaMemberSta.setOrderCount(orderCumStaMemberSta.getOrderCount() + orderVipSta.getVipOrderCount());
            orderCumStaMemberSta.setOrderMoney(new BigDecimal(String.valueOf(orderCumStaMemberSta.getOrderMoney())).add(new BigDecimal(String.valueOf(orderVipSta.getVipOrderMoney()))).floatValue());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("orderCount", orderCumStaMemberSta.getOrderCount());
        map.put("orderMoney", orderCumStaMemberSta.getOrderMoney());
        return JsonResult.succ(map);
    }

    /**
     * 新增用户数,最高气温 折线图
     */
    @RequestMapping("/newUser")
    public JsonResult newUser(Operation operation) throws ParseException {
        Assert.hasLength(operation.getStartTime(), "System.startTime.notNull");
        Assert.hasLength(operation.getEndTime(), "System.endTime.notNull");
        Assert.notNull(operation.getAccountIds(), "AccountUser.agentId.notNull");

        if (operation.getType() == null) {
            operation.setType(1);
        }
        List<NewUserDaySta> newUserDayStaList = null;
        /**日统计*/
        if (operation.getType().equals(1)) {
            if (DateTimeUtil.getYYMMDD(operation.getEndTime()).equals(DateTimeUtil.getNowTimeYMD())) {
                operation.setEndTime(DateTimeUtil.getEndDateTimeBefore(-1));
            }
            newUserDayStaList = newUserDayStaService.getByAccountIdDateDay(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }
        /**周统计*/
        else if (operation.getType().equals(2)) {
            newUserDayStaList = newUserDayStaService.getByAccountIdDateWeek(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }
        /** 月统计*/
        else if (operation.getType().equals(3)) {
            newUserDayStaList = newUserDayStaService.getByAccountIdDateMonth(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }

        /**拼接数据*/
        Map<Integer, List<OperationData>> mapNew = new HashMap<>();
        for (NewUserDaySta newUserDaySta : newUserDayStaList) {
            if (mapNew.get(newUserDaySta.getAccountId()) == null) {
                List<OperationData> operationDataList = new LinkedList<>();
                OperationData<Long> operationData = new OperationData();
                OperationData<Long> operationDataWeather = new OperationData();
                operationData.setName("新增用户数");
                operationDataWeather.setName("最高气温");
                //新增用户数数据
                List<Long> datas = new LinkedList<>();
                Long data = null;
                data = newUserDaySta.getNewUserCount();
                datas.add(data);
                operationData.setData(datas);

                //最高气温数据
                List<Long> weathers = new LinkedList<>();
                Long weather = null;
                weather = newUserDaySta.getNewWeatherCount();
                weathers.add(weather);
                operationDataWeather.setData(weathers);

                //时间
                List<String> times = new LinkedList<>();
                List<String> weeks = new LinkedList<>();
                String time = null;
                String week = null;

                if (operation.getType().equals(1)) {
                    time = newUserDaySta.getNewUserDayDate();
                    times.add(time);
                } else if (operation.getType().equals(2)) {
                    week = newUserDaySta.getDateWeek();
                    time = newUserDaySta.getNewUserDayDate();
                    times.add(time);
                    weeks.add(week);
                } else if (operation.getType().equals(3)) {
                    time = newUserDaySta.getDateMonth();
                    times.add(time);
                }
                operationData.setTime(times);
                operationData.setWeek(weeks);
                operationDataWeather.setTime(times);
                operationDataWeather.setWeek(weeks);

                operationDataList.add(operationData);
                operationDataList.add(operationDataWeather);

                mapNew.put(newUserDaySta.getAccountId(), operationDataList);
            } else {
                mapNew.get(newUserDaySta.getAccountId()).get(0).getData().add(newUserDaySta.getNewUserCount());
                mapNew.get(newUserDaySta.getAccountId()).get(1).getData().add(newUserDaySta.getNewWeatherCount());
                if (operation.getType().equals(1)) {
                    mapNew.get(newUserDaySta.getAccountId()).get(0).getTime().add(newUserDaySta.getNewUserDayDate());
                } else if (operation.getType().equals(2)) {
                    mapNew.get(newUserDaySta.getAccountId()).get(0).getTime().add(newUserDaySta.getNewUserDayDate());
                    mapNew.get(newUserDaySta.getAccountId()).get(0).getWeek().add(newUserDaySta.getDateWeek());
                } else if (operation.getType().equals(3)) {
                    mapNew.get(newUserDaySta.getAccountId()).get(0).getTime().add(newUserDaySta.getDateMonth());
                }
            }
        }

        List<List<OperationData>> operationData = new LinkedList<>();
        for (Integer key : mapNew.keySet()) {
            operationData.add(mapNew.get(key));
        }
        /**补充数据*/
        if (operation.getType().equals(1)) {
            List<String> list = DateTimeUtil.findDates(operation.getStartTime(), operation.getEndTime());

            Map<String, Integer> listDate = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });
            for (String i : list) {
                listDate.put(i, 0);
            }
            Map<String, Integer> listWeather = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });

            for (String i : list) {
                listDate.put(i, 0);
                listWeather.put(i, 0);
            }

            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<Integer> weather = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                date = operationData.get(0).get(0).getData();
                weather = operationData.get(0).get(1).getData();
            }

            if (time.size() != listDate.size()) {
                Map<String, Integer> map = new TreeMap<>();
                Map<String, Integer> mapWeather = new TreeMap<>();
                for (int i = 0; i < date.size(); i++) {
                    map.put(time.get(i), date.get(i));
                    mapWeather.put(time.get(i), weather.get(i));
                }
                listDate.putAll(map);
                listWeather.putAll(mapWeather);
                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Integer> weatherMap = new LinkedList<>();
                for (String key : listDate.keySet()) {
                    timeMap.add(key);
                    dateMap.add(listDate.get(key));
                    weatherMap.add(listWeather.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("新增用户数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Integer> operationData2 = new OperationData<>();
                    operationData2.setName("最高气温");
                    operationData2.setData(weatherMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);

                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(weatherMap);
                }
            }
        } else if (operation.getType().equals(2)) {
            Map<String, String> listTime = DateTimeUtil.getDayOfWeekWithinDateInterval(operation.getStartTime(), operation.getEndTime(), 1);
            Map<String, Integer> listDate = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });
            Map<String, Integer> listWeather = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });

            for (String key : listTime.keySet()) {
                listDate.put(key, 0);
                listWeather.put(key, 0);
            }

            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<String> week = new LinkedList<>();
            List<Integer> weather = new ArrayList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                week = operationData.get(0).get(0).getWeek();
                date = operationData.get(0).get(0).getData();
                weather = operationData.get(0).get(1).getData();
            }

            if (time.size() != listDate.size()) {
                Map<String, String> mapWeekTime = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < week.size(); i++) {
                    mapWeekTime.put(week.get(i), time.get(i));
                }
                Map<String, Integer> mapWeekDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Integer> mapWeekWeather = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < week.size(); i++) {
                    mapWeekDate.put(week.get(i), date.get(i));
                    mapWeekWeather.put(week.get(i), weather.get(i));
                }
                listDate.putAll(mapWeekDate);
                listWeather.putAll(mapWeekWeather);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Integer> weatherMap = new LinkedList<>();
                for (String key : listTime.keySet()) {
                    timeMap.add(listTime.get(key));
                    dateMap.add(listDate.get(key));
                    weatherMap.add(listWeather.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("新增用户数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Integer> operationData2 = new OperationData<>();
                    operationData2.setName("最高气温");
                    operationData2.setData(weatherMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(weatherMap);
                }
            }
        } else if (operation.getType().equals(3)) {
            List<String> monthDay = DateTimeUtil.getMonthDay(operation.getStartTime(), operation.getEndTime());
            LogUtil.info("新增用户数,最高气温 折线图：" + monthDay.toString());
            Map<String, Integer> montData = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });
            Map<String, Integer> weatherData = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });
            for (String k : monthDay) {
                montData.put(k, 0);
                weatherData.put(k, 0);
            }
            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<Integer> weather = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                date = operationData.get(0).get(0).getData();
                weather = operationData.get(0).get(1).getData();
            }

            if (time.size() != monthDay.size()) {
                Map<String, Integer> map = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Integer> mapWeather = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < date.size(); i++) {
                    map.put(time.get(i), date.get(i));
                    mapWeather.put(time.get(i), weather.get(i));
                }
                montData.putAll(map);
                weatherData.putAll(mapWeather);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Integer> weatherMap = new ArrayList<>();
                for (String key : montData.keySet()) {
                    timeMap.add(key);
                    dateMap.add(montData.get(key));
                    weatherMap.add(weatherData.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("新增用户数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Integer> operationData2 = new OperationData<>();
                    operationData2.setName("最高气温");
                    operationData2.setData(weatherMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(weatherMap);
                }
            }
        }
        return JsonResult.succ(operationData);
    }

    /**
     * 骑行订单统计 折线图
     */
    @RequestMapping("/rideOrder")
    public JsonResult rideOrder(Operation operation) {

        Assert.hasLength(operation.getStartTime(), "System.startTime.notNull");
        Assert.hasLength(operation.getEndTime(), "System.endTime.notNull");
        Assert.notNull(operation.getAccountIds(), "AccountUser.agentId.notNull");

        if (operation.getType() == null) {
            operation.setType(1);
        }
        List<RideOrderDaySta> rideOrderDayStaList = null;
        /**日统计*/
        if (operation.getType().equals(1)) {
            if (DateTimeUtil.getYYMMDD(operation.getEndTime()).equals(DateTimeUtil.getNowTimeYMD())) {
                operation.setEndTime(DateTimeUtil.getEndDateTimeBefore(-1));
            }
            rideOrderDayStaList = rideOrderDayStaService.getByAccountIdDateDay(operation.getAccountIds(), null, operation.getStartTime(), operation.getEndTime());
        }
        /**周统计*/
        else if (operation.getType().equals(2)) {
            rideOrderDayStaList = rideOrderDayStaService.getByAccountIdDateWeek(operation.getAccountIds(), null, operation.getStartTime(), operation.getEndTime());
        }
        /** 月统计*/
        else if (operation.getType().equals(3)) {
            rideOrderDayStaList = rideOrderDayStaService.getByAccountIdDateMonth(operation.getAccountIds(), null, operation.getStartTime(), operation.getEndTime());
        }

        /**拼接数据*/
        //虚拟的id 多个区域聚合时,后面所有的数据都往第一个区域id上加
        Integer virtuallyAccountId = 888;
        Map<Integer, List<OperationData>> mapNew = new HashMap<>();
        for (RideOrderDaySta rideOrderDaySta : rideOrderDayStaList) {
            if (mapNew.size() == 0) {
                List<OperationData> operationDataList = new LinkedList<>();
                OperationData<Long> operationDataCount = new OperationData<>();
                OperationData<Float> operationDataMoney = new OperationData<>();
                operationDataCount.setName("骑行订单数");
                operationDataMoney.setName("骑行订单金额");
                //骑行订单数
                List<Long> dataCounts = new LinkedList<>();
                Long dataCount = null;
                DecimalFormat df = new DecimalFormat("0.00");
                dataCount = rideOrderDaySta.getOrderCount();
                dataCounts.add(dataCount);
                operationDataCount.setData(dataCounts);

                //订单金额
                List<Float> dataMoneys = new LinkedList<>();
                Float dataMoney = null;
                dataMoney = Float.parseFloat(df.format((float) rideOrderDaySta.getOrderMoney().intValue() / 100));
                dataMoneys.add(dataMoney);
                operationDataMoney.setData(dataMoneys);

                //时间
                List<String> times = new LinkedList<>();
                List<String> weeks = new LinkedList<>();
                if (operation.getType().equals(1)) {
                    times.add(rideOrderDaySta.getOrderDate());
                } else if (operation.getType().equals(2)) {
                    times.add(rideOrderDaySta.getOrderDate());
                    weeks.add(rideOrderDaySta.getDateWeek());
                } else if (operation.getType().equals(3)) {
                    times.add(rideOrderDaySta.getDateMonth());
                }

                operationDataCount.setTime(times);
                operationDataCount.setWeek(weeks);
                operationDataMoney.setTime(times);
                operationDataMoney.setWeek(weeks);

                operationDataList.add(operationDataCount);
                operationDataList.add(operationDataMoney);
                mapNew.put(rideOrderDaySta.getAccountId(), operationDataList);
                virtuallyAccountId = rideOrderDaySta.getAccountId();
            } else {
                DecimalFormat df = new DecimalFormat("0.00");
                mapNew.get(virtuallyAccountId).get(0).getData().add(rideOrderDaySta.getOrderCount());
                mapNew.get(virtuallyAccountId).get(1).getData().add(Float.parseFloat(df.format((float) rideOrderDaySta.getOrderMoney().intValue() / 100)));
                if (operation.getType().equals(1)) {
                    mapNew.get(virtuallyAccountId).get(0).getTime().add(rideOrderDaySta.getOrderDate());
                } else if (operation.getType().equals(2)) {
                    mapNew.get(virtuallyAccountId).get(0).getTime().add(rideOrderDaySta.getOrderDate());
                    mapNew.get(virtuallyAccountId).get(0).getWeek().add(rideOrderDaySta.getDateWeek());
                } else if (operation.getType().equals(3)) {
                    mapNew.get(virtuallyAccountId).get(0).getTime().add(rideOrderDaySta.getDateMonth());
                }
            }
        }

        List<List<OperationData>> operationData = new LinkedList<>();
        for (Integer key : mapNew.keySet()) {
            operationData.add(mapNew.get(key));
        }
        /**补充数据*/
        if (operation.getType().equals(1)) {
            List<String> list = DateTimeUtil.findDates(operation.getStartTime(), operation.getEndTime());

            Map<String, Integer> listDate = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    //升序排序
                    return obj1.compareTo(obj2);
                }
            });
            Map<String, Float> listMoney = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    //升序排序
                    return obj1.compareTo(obj2);
                }
            });
            for (String i : list) {
                listDate.put(i, 0);
                listMoney.put(i, (float) 0.00);
            }

            List<String> time = new LinkedList<>();
            List<Integer> dateCount = new LinkedList<>();
            List<Float> dateMoney = new LinkedList<>();

            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                dateCount = operationData.get(0).get(0).getData();
                dateMoney = operationData.get(0).get(1).getData();
            }

            if (time.size() != listDate.size()) {
                Map<String, Integer> mapCount = new TreeMap<>();
                Map<String, Float> mapMoney = new TreeMap<>();
                for (int i = 0; i < dateCount.size(); i++) {
                    mapCount.put(time.get(i), dateCount.get(i));
                    mapMoney.put(time.get(i), dateMoney.get(i));
                }
                listDate.putAll(mapCount);
                listMoney.putAll(mapMoney);
                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : listDate.keySet()) {
                    timeMap.add(key);
                    dateMap.add(listDate.get(key));
                    moneyMap.add(listMoney.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("骑行订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("骑行订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        } else if (operation.getType().equals(2)) {
            Map<String, String> listTime = DateTimeUtil.getDayOfWeekWithinDateInterval(operation.getStartTime(), operation.getEndTime(), 1);

            Map<String, Integer> listDate = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });
            Map<String, Float> listMoney = new TreeMap<>(new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    return obj1.compareTo(obj2);
                }
            });
            for (String key : listTime.keySet()) {
                listDate.put(key, 0);
                listMoney.put(key, (float) 0.00);
            }

            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<String> week = new LinkedList<>();
            List<Float> money = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                week = operationData.get(0).get(0).getWeek();
                date = operationData.get(0).get(0).getData();
                money = operationData.get(0).get(1).getData();
            }

            if (time.size() != listTime.size()) {

                Map<String, String> mapWeekTime = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });

                for (int i = 0; i < week.size(); i++) {
                    mapWeekTime.put(week.get(i), time.get(i));
                }
                //listTime.putAll(mapWeekTime);

                Map<String, Integer> mapWeekDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapWeekMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < week.size(); i++) {
                    mapWeekDate.put(week.get(i), date.get(i));
                    mapWeekMoney.put(week.get(i), money.get(i));
                }
                listDate.putAll(mapWeekDate);
                listMoney.putAll(mapWeekMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : listTime.keySet()) {
                    timeMap.add(listTime.get(key));
                    dateMap.add(listDate.get(key));
                    moneyMap.add(listMoney.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("骑行订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("骑行订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        } else if (operation.getType().equals(3)) {
            List<String> monthDay = DateTimeUtil.getMonthDay(operation.getStartTime(), operation.getEndTime());

            LogUtil.info("骑行订单：" + monthDay.toString());
            Map<String, Integer> montData = new TreeMap<>();
            Map<String, Float> moneyData = new TreeMap<>();
            for (String k : monthDay) {
                montData.put(k, 0);
                moneyData.put(k, (float) 0.00);
            }
            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<Float> money = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                date = operationData.get(0).get(0).getData();
                money = operationData.get(0).get(1).getData();
            }


            if (time.size() != monthDay.size()) {
                Map<String, Integer> mapDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < date.size(); i++) {
                    mapDate.put(time.get(i), date.get(i));
                    mapMoney.put(time.get(i), money.get(i));
                }
                montData.putAll(mapDate);
                moneyData.putAll(mapMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : montData.keySet()) {
                    timeMap.add(key);
                    dateMap.add(montData.get(key));
                    moneyMap.add(moneyData.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("骑行订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("骑行订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        }
        return JsonResult.succ(operationData);
    }

    /**
     * 免押订单统计 折线图
     */
    @RequestMapping("/depositOrder")
    public JsonResult depositOrder(Operation operation) throws ParseException {

        Assert.hasLength(operation.getStartTime(), "System.startTime.notNull");
        Assert.hasLength(operation.getEndTime(), "System.endTime.notNull");
        Assert.notNull(operation.getAccountIds(), "AccountUser.agentId.notNull");

        if (operation.getType() == null) {
            operation.setType(1);
        }
        List<DepositOrderDaySta> depositOrderDayStaList = null;
        /**日统计*/
        if (operation.getType().equals(1)) {
            if (DateTimeUtil.getYYMMDD(operation.getEndTime()).equals(DateTimeUtil.getNowTimeYMD())) {
                operation.setEndTime(DateTimeUtil.getEndDateTimeBefore(-1));
            }
            depositOrderDayStaList = depositOrderDayStaService.getByAccountIdDateDay(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }
        /**周统计*/
        else if (operation.getType().equals(2)) {
            depositOrderDayStaList = depositOrderDayStaService.getByAccountIdDateWeek(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }
        /** 月统计*/
        else if (operation.getType().equals(3)) {
            depositOrderDayStaList = depositOrderDayStaService.getByAccountIdDateMonth(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }

        /**拼接数据*/
        Map<Integer, List<OperationData>> mapNew = new HashMap<>();
        for (DepositOrderDaySta depositOrderDaySta : depositOrderDayStaList) {
            if (mapNew.get(depositOrderDaySta.getAccountId()) == null) {
                List<OperationData> operationDataList = new LinkedList<>();
                OperationData<Float> operationDataCount = new OperationData();
                OperationData<Float> operationDataMoney = new OperationData();
                operationDataCount.setName("免押订单数");
                operationDataMoney.setName("免押订单金额");
                //订单
                List<Float> dataCounts = new LinkedList<>();
                Float dataCount = null;
                DecimalFormat df = new DecimalFormat("0.00");
                dataCount = Float.parseFloat(df.format((float) depositOrderDaySta.getOrderCount().intValue()));
                dataCounts.add(dataCount);
                operationDataCount.setData(dataCounts);

                //金额
                List<Float> dataMoneys = new LinkedList<>();
                Float dataMoney = null;
                dataMoney = Float.parseFloat(df.format((float) depositOrderDaySta.getOrderMoney().intValue() / 100));
                dataMoneys.add(dataMoney);
                operationDataMoney.setData(dataMoneys);

                //时间
                List<String> times = new LinkedList<>();
                List<String> weeks = new LinkedList<>();
                String time = null;
                String week = null;
                if (operation.getType().equals(1)) {
                    time = depositOrderDaySta.getOrderDate();
                    times.add(time);
                } else if (operation.getType().equals(2)) {
                    time = depositOrderDaySta.getOrderDate();
                    week = depositOrderDaySta.getDateWeek();
                    times.add(time);
                    weeks.add(week);
                } else if (operation.getType().equals(3)) {
                    time = depositOrderDaySta.getDateMonth();
                    times.add(time);
                }

                operationDataCount.setTime(times);
                operationDataCount.setWeek(weeks);
                operationDataMoney.setTime(times);
                operationDataMoney.setWeek(weeks);
                operationDataList.add(operationDataCount);
                operationDataList.add(operationDataMoney);
                mapNew.put(depositOrderDaySta.getAccountId(), operationDataList);
            } else {
                DecimalFormat df = new DecimalFormat("0.00");
                mapNew.get(depositOrderDaySta.getAccountId()).get(0).getData().add(depositOrderDaySta.getOrderCount());
                mapNew.get(depositOrderDaySta.getAccountId()).get(1).getData().add(Float.parseFloat(df.format((float) depositOrderDaySta.getOrderMoney().intValue() / 100)));
                if (operation.getType().equals(1)) {
                    mapNew.get(depositOrderDaySta.getAccountId()).get(0).getTime().add(depositOrderDaySta.getOrderDate());
                } else if (operation.getType().equals(2)) {
                    mapNew.get(depositOrderDaySta.getAccountId()).get(0).getTime().add(depositOrderDaySta.getOrderDate());
                    mapNew.get(depositOrderDaySta.getAccountId()).get(0).getWeek().add(depositOrderDaySta.getDateWeek());
                } else if (operation.getType().equals(3)) {
                    mapNew.get(depositOrderDaySta.getAccountId()).get(0).getTime().add(depositOrderDaySta.getDateMonth());
                }
            }
        }

        List<List<OperationData>> operationData = new LinkedList<>();
        for (Integer key : mapNew.keySet()) {
            operationData.add(mapNew.get(key));
        }

        /**补充数据*/
        if (operation.getType().equals(1)) {
            List<String> list = DateTimeUtil.findDates(operation.getStartTime(), operation.getEndTime());

            Map<String, Integer> listDate = new TreeMap<>();
            Map<String, Float> listMoney = new TreeMap<>();
            for (String i : list) {
                listDate.put(i, 0);
                listMoney.put(i, (float) 0.00);
            }

            List<String> time = new LinkedList<>();
            List<Integer> dateCount = new LinkedList<>();
            List<Float> dateMoney = new LinkedList<>();

            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                dateCount = operationData.get(0).get(0).getData();
                dateMoney = operationData.get(0).get(1).getData();
            }

            if (time.size() != listDate.size()) {
                Map<String, Integer> mapCount = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < dateCount.size(); i++) {
                    mapCount.put(time.get(i), dateCount.get(i));
                    mapMoney.put(time.get(i), dateMoney.get(i));
                }
                listDate.putAll(mapCount);
                listMoney.putAll(mapMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : listDate.keySet()) {
                    timeMap.add(key);
                    dateMap.add(listDate.get(key));
                    moneyMap.add(listMoney.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("免押订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("免押订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        } else if (operation.getType().equals(2)) {
            Map<String, String> listTime = DateTimeUtil.getDayOfWeekWithinDateInterval(operation.getStartTime(), operation.getEndTime(), 1);

            Map<String, Integer> listDate = new TreeMap<>();
            Map<String, Float> listMoney = new TreeMap<>();
            for (String key : listTime.keySet()) {
                listDate.put(key, 0);
                listMoney.put(key, (float) 0.00);
            }

            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<String> week = new LinkedList<>();
            List<Float> money = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                week = operationData.get(0).get(0).getWeek();
                date = operationData.get(0).get(0).getData();
                money = operationData.get(0).get(1).getData();
            }

            if (time.size() != listTime.size()) {

                Map<String, String> mapWeekTime = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });

                for (int i = 0; i < week.size(); i++) {
                    mapWeekTime.put(week.get(i), time.get(i));
                }
                //listTime.putAll(mapWeekTime);

                Map<String, Integer> mapWeekDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapWeekMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });

                for (int i = 0; i < week.size(); i++) {
                    mapWeekDate.put(week.get(i), date.get(i));
                    mapWeekMoney.put(week.get(i), money.get(i));
                }
                listDate.putAll(mapWeekDate);
                listMoney.putAll(mapWeekMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : listTime.keySet()) {
                    timeMap.add(listTime.get(key));
                    dateMap.add(listDate.get(key));
                    moneyMap.add(listMoney.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("免押订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("免押订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        } else if (operation.getType().equals(3)) {
            List<String> monthDay = DateTimeUtil.getMonthDay(operation.getStartTime(), operation.getEndTime());
            Map<String, Integer> montData = new TreeMap<>();
            Map<String, Float> moneyData = new TreeMap<>();
            for (String k : monthDay) {
                montData.put(k, 0);
                moneyData.put(k, (float) 0.00);
            }
            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<Float> money = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                date = operationData.get(0).get(0).getData();
                money = operationData.get(0).get(1).getData();
            }

            if (time.size() != monthDay.size()) {
                Map<String, Integer> mapDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < date.size(); i++) {
                    mapDate.put(time.get(i), date.get(i));
                    mapMoney.put(time.get(i), money.get(i));
                }
                montData.putAll(mapDate);
                moneyData.putAll(mapMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : montData.keySet()) {
                    timeMap.add(key);
                    dateMap.add(montData.get(key));
                    moneyMap.add(moneyData.get(key));
                }

                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("免押订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("免押订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        }

        return JsonResult.succ(operationData);
    }

    /**
     * 会员卡订单统计 折线图
     */
    @RequestMapping("/vipCardOrder")
    public JsonResult vipCardOrder(Operation operation) throws ParseException {

        Assert.hasLength(operation.getStartTime(), "System.startTime.notNull");
        Assert.hasLength(operation.getEndTime(), "System.endTime.notNull");
        Assert.notNull(operation.getAccountIds(), "AccountUser.agentId.notNull");

        if (operation.getType() == null) {
            operation.setType(1);
        }
        List<VipCardOrderDaySta> vipCardOrderDayStaList = null;
        /**日统计*/
        if (operation.getType().equals(1)) {
            if (DateTimeUtil.getYYMMDD(operation.getEndTime()).equals(DateTimeUtil.getNowTimeYMD())) {
                operation.setEndTime(DateTimeUtil.getEndDateTimeBefore(-1));
            }
            vipCardOrderDayStaList = vipCardOrderDayStaService.getByAccountIdDateDay(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }
        /**周统计*/
        else if (operation.getType().equals(2)) {
            vipCardOrderDayStaList = vipCardOrderDayStaService.getByAccountIdDateWeek(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }
        /** 月统计*/
        else if (operation.getType().equals(3)) {
            vipCardOrderDayStaList = vipCardOrderDayStaService.getByAccountIdDateMonth(operation.getAccountIds(), operation.getStartTime(), operation.getEndTime());
        }

        /**拼接数据*/
        Map<Integer, List<OperationData>> mapNew = new HashMap<>();
        for (VipCardOrderDaySta vipCardOrderDaySta : vipCardOrderDayStaList) {
            if (mapNew.get(vipCardOrderDaySta.getAccountId()) == null) {
                List<OperationData> operationDataList = new LinkedList<>();
                OperationData<Float> operationDataCount = new OperationData();
                OperationData<Float> operationDataMoney = new OperationData();
                operationDataCount.setName("会员卡订单数");
                operationDataMoney.setName("会员卡订单金额");
                //订单
                List<Float> dataCounts = new LinkedList<>();
                Float dataCount = null;
                DecimalFormat df = new DecimalFormat("0.00");
                dataCount = Float.parseFloat(df.format((float) vipCardOrderDaySta.getOrderCount().intValue()));
                dataCounts.add(dataCount);
                operationDataCount.setData(dataCounts);

                //金额
                List<Float> dataMoneys = new LinkedList<>();
                Float dataMoney = null;
                dataMoney = Float.parseFloat(df.format((float) vipCardOrderDaySta.getOrderMoney().intValue() / 100));
                dataMoneys.add(dataMoney);
                operationDataMoney.setData(dataMoneys);

                //时间
                List<String> times = new LinkedList<>();
                List<String> weeks = new LinkedList<>();
                String time = null;
                String week = null;
                if (operation.getType().equals(1)) {
                    time = vipCardOrderDaySta.getOrderDate();
                    times.add(time);
                } else if (operation.getType().equals(2)) {
                    time = vipCardOrderDaySta.getOrderDate();
                    week = vipCardOrderDaySta.getDateWeek();
                    times.add(time);
                    weeks.add(week);
                } else if (operation.getType().equals(3)) {
                    time = vipCardOrderDaySta.getDateMonth();
                    times.add(time);
                }

                operationDataCount.setTime(times);
                operationDataCount.setWeek(weeks);
                operationDataMoney.setTime(times);
                operationDataMoney.setWeek(weeks);
                operationDataList.add(operationDataCount);
                operationDataList.add(operationDataMoney);
                mapNew.put(vipCardOrderDaySta.getAccountId(), operationDataList);
            } else {
                DecimalFormat df = new DecimalFormat("0.00");
                mapNew.get(vipCardOrderDaySta.getAccountId()).get(0).getData().add(vipCardOrderDaySta.getOrderCount());
                mapNew.get(vipCardOrderDaySta.getAccountId()).get(1).getData().add(Float.parseFloat(df.format((float) vipCardOrderDaySta.getOrderMoney().intValue() / 100)));
                if (operation.getType().equals(1)) {
                    mapNew.get(vipCardOrderDaySta.getAccountId()).get(0).getTime().add(vipCardOrderDaySta.getOrderDate());
                } else if (operation.getType().equals(2)) {
                    mapNew.get(vipCardOrderDaySta.getAccountId()).get(0).getTime().add(vipCardOrderDaySta.getOrderDate());
                    mapNew.get(vipCardOrderDaySta.getAccountId()).get(0).getWeek().add(vipCardOrderDaySta.getDateWeek());
                } else if (operation.getType().equals(3)) {
                    mapNew.get(vipCardOrderDaySta.getAccountId()).get(0).getTime().add(vipCardOrderDaySta.getDateMonth());
                }
            }
        }

        List<List<OperationData>> operationData = new LinkedList<>();
        for (Integer key : mapNew.keySet()) {
            operationData.add(mapNew.get(key));
        }

        /**补充数据*/
        if (operation.getType().equals(1)) {
            List<String> list = DateTimeUtil.findDates(operation.getStartTime(), operation.getEndTime());

            Map<String, Integer> listDate = new TreeMap<>();
            Map<String, Float> listMoney = new TreeMap<>();
            for (String i : list) {
                listDate.put(i, 0);
                listMoney.put(i, (float) 0.00);
            }

            List<String> time = new LinkedList<>();
            List<Integer> dateCount = new LinkedList<>();
            List<Float> dateMoney = new LinkedList<>();

            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                dateCount = operationData.get(0).get(0).getData();
                dateMoney = operationData.get(0).get(1).getData();
            }

            if (time.size() != listDate.size()) {
                Map<String, Integer> mapCount = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < dateCount.size(); i++) {
                    mapCount.put(time.get(i), dateCount.get(i));
                    mapMoney.put(time.get(i), dateMoney.get(i));
                }
                listDate.putAll(mapCount);
                listMoney.putAll(mapMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : listDate.keySet()) {
                    timeMap.add(key);
                    dateMap.add(listDate.get(key));
                    moneyMap.add(listMoney.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("会员卡订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("会员卡订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        } else if (operation.getType().equals(2)) {
            Map<String, String> listTime = DateTimeUtil.getDayOfWeekWithinDateInterval(operation.getStartTime(), operation.getEndTime(), 1);

            Map<String, Integer> listDate = new TreeMap<>();
            Map<String, Float> listMoney = new TreeMap<>();
            for (String key : listTime.keySet()) {
                listDate.put(key, 0);
                listMoney.put(key, (float) 0.00);
            }

            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<String> week = new LinkedList<>();
            List<Float> money = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                week = operationData.get(0).get(0).getWeek();
                date = operationData.get(0).get(0).getData();
                money = operationData.get(0).get(1).getData();
            }

            if (time.size() != listTime.size()) {

                Map<String, String> mapWeekTime = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });

                for (int i = 0; i < week.size(); i++) {
                    mapWeekTime.put(week.get(i), time.get(i));
                }
                Map<String, Integer> mapWeekDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapWeekMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });

                for (int i = 0; i < week.size(); i++) {
                    mapWeekDate.put(week.get(i), date.get(i));
                    mapWeekMoney.put(week.get(i), money.get(i));
                }
                listDate.putAll(mapWeekDate);
                listMoney.putAll(mapWeekMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : listTime.keySet()) {
                    timeMap.add(listTime.get(key));
                    dateMap.add(listDate.get(key));
                    moneyMap.add(listMoney.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("会员卡订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("会员卡订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        } else if (operation.getType().equals(3)) {
            List<String> monthDay = DateTimeUtil.getMonthDay(operation.getStartTime(), operation.getEndTime());
            Map<String, Integer> montData = new TreeMap<>();
            Map<String, Float> moneyData = new TreeMap<>();
            for (String k : monthDay) {
                montData.put(k, 0);
                moneyData.put(k, (float) 0.00);
            }
            List<String> time = new LinkedList<>();
            List<Integer> date = new LinkedList<>();
            List<Float> money = new LinkedList<>();
            if (operationData.size() > 0) {
                time = operationData.get(0).get(0).getTime();
                date = operationData.get(0).get(0).getData();
                money = operationData.get(0).get(1).getData();
            }

            if (time.size() != monthDay.size()) {
                Map<String, Integer> mapDate = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                Map<String, Float> mapMoney = new TreeMap<>(new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return obj1.compareTo(obj2);
                    }
                });
                for (int i = 0; i < date.size(); i++) {
                    mapDate.put(time.get(i), date.get(i));
                    mapMoney.put(time.get(i), money.get(i));
                }
                montData.putAll(mapDate);
                moneyData.putAll(mapMoney);

                List<String> timeMap = new LinkedList<>();
                List<Integer> dateMap = new LinkedList<>();
                List<Float> moneyMap = new LinkedList<>();
                for (String key : montData.keySet()) {
                    timeMap.add(key);
                    dateMap.add(montData.get(key));
                    moneyMap.add(moneyData.get(key));
                }
                if (operationData.size() == 0) {
                    List<OperationData> operationDataList = new LinkedList<>();
                    OperationData<Integer> operationData1 = new OperationData<>();
                    operationData1.setName("会员卡订单数");
                    operationData1.setData(dateMap);
                    operationData1.setTime(timeMap);
                    operationDataList.add(operationData1);
                    OperationData<Float> operationData2 = new OperationData<>();
                    operationData2.setName("会员卡订单金额");
                    operationData2.setData(moneyMap);
                    operationData2.setTime(timeMap);
                    operationDataList.add(operationData2);
                    operationData.add(operationDataList);
                } else {
                    operationData.get(0).get(0).setTime(timeMap);
                    operationData.get(0).get(0).setData(dateMap);
                    operationData.get(0).get(1).setTime(timeMap);
                    operationData.get(0).get(1).setData(moneyMap);
                }
            }
        }

        return JsonResult.succ(operationData);
    }

    /**
     * 分时统计
     */
    @RequestMapping("/hour")
    public JsonResult hour(String token, Operation operation) {

        Assert.notNull(operation.getAccountIds(), "AccountUser.agentId.notNull");

        List<OperationHourSta> operationHourStaList = operationHourStaService.getByAccountIdDate(operation.getAccountIds());

        List<OperationHourSta> operationHourStaSub = new LinkedList<>();
        for (int i = 0; i < operationHourStaList.size(); i++) {
            if (operationHourStaList.get(i).getOperationHour() > DateTimeUtil.getNewHour()) {
                operationHourStaSub.add(operationHourStaList.get(i));
            }
        }
        for (int i = 0; i < operationHourStaList.size(); i++) {
            if (operationHourStaList.get(i).getOperationHour() <= DateTimeUtil.getNewHour()) {
                operationHourStaSub.add(operationHourStaList.get(i));
            }
        }

        List<Integer> useMachineCounts = new LinkedList<>();
        List<Integer> ridingMachineCounts = new LinkedList<>();
        List<Integer> lackPowerMachineCounts = new LinkedList<>();
        List<Integer> lackParkpointCounts = new ArrayList<>();
        List<Integer> lackFaultCounts = new ArrayList<>();
        List<Integer> lackStateCounts = new ArrayList<>();
        List<Integer> lackBorrowCounts = new ArrayList<>();
        List<String> times = new LinkedList<>();

        for (int i = 0; i < operationHourStaSub.size(); i++) {
            if (operationHourStaSub.get(i).getOperationHour().equals(DateTimeUtil.getNewHour())) {

                /** 获取缺电数*/
                AccountConfig accountConfig = accountConfigService.getByAccountId(operationHourStaSub.get(i).getAccountId());
                List<String> lackMachineNo = new LinkedList<>();
                if (accountConfig == null) {
                    lackPowerMachineCounts.add(0);
                } else {
                    //缺电车辆
                    List<MachineStatus> list = machineStatusService.getSocByAccountId(operationHourStaSub.get(i).getAccountId(), accountConfig.getBorrowSocLimit());
                    if (list != null) {
                        for (MachineStatus machine : list) {
                            lackMachineNo.add(machine.getMachineNO());
                        }
                        lackPowerMachineCounts.add(list.size());
                    } else {
                        lackPowerMachineCounts.add(0);
                    }
                }

                //异常车辆
                List<MachineAbnormal> machineAbnormals = machineAbnormalService.getByAccountId(operationHourStaSub.get(i).getAccountId());
                List<String> abnormalMachineNo = new LinkedList<>();
                if (machineAbnormals != null) {
                    for (MachineAbnormal machine : machineAbnormals) {
                        abnormalMachineNo.add(machine.getMachineNO());
                    }
                }


                /** 正在运营车辆*/
                Integer machineNormal = machineService.getByAccountIdType(operationHourStaSub.get(i).getAccountId(), MachineStateConstant.MACHINE_STATE_ZCYY);

                /** 可运营车辆数*/
                Integer machineUserCount = 0;
                lackMachineNo.removeAll(abnormalMachineNo);
                lackMachineNo.addAll(abnormalMachineNo);
                machineUserCount = machineNormal - lackMachineNo.size();
                if (machineUserCount < 0) {
                    machineUserCount = 0;
                }
                useMachineCounts.add(machineUserCount);

                /** 正在骑行车辆数*/
                Integer machineUseIng = machineBorrowService.getByAccountIdCount(operationHourStaSub.get(i).getAccountId());
                ridingMachineCounts.add(machineUseIng);


                //无信号车辆数
                Integer machineWXH = machineAbnormalService.getAccountIdTypeCount(operationHourStaSub.get(i).getAccountId(), MachineAbnormalConstant.MACHINE_ABNORMAL_WXH);
                lackStateCounts.add(machineWXH);

                //站外车辆数
                Integer machineZW = machineAbnormalService.getAccountIdTypeCount(operationHourStaSub.get(i).getAccountId(), MachineAbnormalConstant.MACHINE_ABNORMAL_ZW);
                lackParkpointCounts.add(machineZW);

                //运维故障数
                Integer lackFault = machineFaultService.getByAccountIdType(operationHourStaSub.get(i).getAccountId(), 0);
                lackFaultCounts.add(lackFault);

                //闲置车辆数
                List<MachineStatusBorrow> machineStatusBorrows = machineStatusService.getUnUseByAccountId(operationHourStaSub.get(i).getAccountId(), DateTimeUtil.getHoursAgo(24));
                lackBorrowCounts.add(machineStatusBorrows.size());

                /** 获取时间*/
                times.add(DateTimeUtil.getNewHour() + "");
            } else {
                //可运营车俩数
                Integer useMachineCount = operationHourStaSub.get(i).getUseMachineCount();
                useMachineCounts.add(useMachineCount);

                //在骑车辆数
                Integer ridingMachineCount = operationHourStaSub.get(i).getRidingMachineCount();
                ridingMachineCounts.add(ridingMachineCount);

                //缺电车辆数
                Integer lackPowerMachineCount = operationHourStaSub.get(i).getLackPowerMachineCount();
                lackPowerMachineCounts.add(lackPowerMachineCount);

                //无信号车辆数
                Integer lackStateMachineCount = operationHourStaSub.get(i).getLackState();
                lackStateCounts.add(lackStateMachineCount);

                //站外车辆数
                Integer lackParkPointMachineCount = operationHourStaSub.get(i).getLackParkpoint();
                lackParkpointCounts.add(lackParkPointMachineCount);

                //运维故障车辆数
                Integer lackFaultMachineCount = operationHourStaSub.get(i).getLackFault();
                lackFaultCounts.add(lackFaultMachineCount);

                //闲置车辆数
                Integer lackLastMachineCount = operationHourStaSub.get(i).getLackBorrow();
                lackBorrowCounts.add(lackLastMachineCount);

                //时间
                String time = operationHourStaSub.get(i).getOperationHour() + "";
                times.add(time);
            }
        }

        List<OperationData> map = new LinkedList<>();
        map.add(new OperationData("可运营车辆数", useMachineCounts, times));
        map.add(new OperationData("在骑车辆数", ridingMachineCounts, times));
        map.add(new OperationData("缺电车辆数", lackPowerMachineCounts, times));
        map.add(new OperationData("无信号车辆数", lackStateCounts, times));
        map.add(new OperationData("站外车辆数", lackParkpointCounts, times));
        map.add(new OperationData("运维故障车辆", lackFaultCounts, times));
        map.add(new OperationData("闲置车辆数", lackBorrowCounts, times));
        return JsonResult.succ(map);
    }

}
